//
//  KLMTabBarController.swift
//  KLM_GN
//
//  Created by Nic on 2025/2/18.
//

import UIKit

// MARK: - 参数配置结构体
struct TabBarItemConfig {
    let vc: String
    let title: String
    let image: String
    let selectedImage: String
    
    init?(from dict: [String: String]) {
        guard let vc = dict["vc"],
              let title = dict["title"],
              let imageName = dict["image"],
              let selectedImageName = dict["selectedImage"] else {
            return nil
        }
        self.vc = vc
        self.title = title
        self.image = imageName
        self.selectedImage = selectedImageName
    }
}


class KLMTabBarController: UITabBarController {

    // MARK: - 懒加载配置数组
    lazy var tabBarConfigs: [[String: String]] = {
        return [
            [
                "vc": "KLMHomeController",
                "title": KLMLocalizable.LANGLOC("Home"),
                "image": "TabbarItem_home",
                "selectedImage": "TabbarItem_home_select"
            ],
            [
                "vc": "KLMRoomController",
                "title": KLMLocalizable.LANGLOC("Room"),
                "image": "TabbarItem_room",
                "selectedImage": "TabbarItem_room_select"
            ],
            [
                "vc": "KLMSceneController",
                "title": KLMLocalizable.LANGLOC("Scene"),
                "image": "TabbarItem_scene",
                "selectedImage": "TabbarItem_scene_select"
            ],
            [
                "vc": "KLMIntelligenceController",
                "title": KLMLocalizable.LANGLOC("Intelligence"),
                "image": "TabbarItem_intelligence",
                "selectedImage": "TabbarItem_intelligence_select"
            ],
            [
                "vc": "KLMMineController",
                "title": KLMLocalizable.LANGLOC("Mine"),
                "image": "TabbarItem_mine",
                "selectedImage": "TabbarItem_mine_select"
            ]
        ]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initConfigs()
    }
    
    private func initConfigs() {
        
        tabBar.barTintColor = appBackGroupColor
        
        tabBarConfigs.forEach { dict in
            if let config = TabBarItemConfig(from: dict) {
                addChildVc(config: config)
            }
        }
    }
    
    // MARK: - 添加子控制器方法
    private func addChildVc(config: TabBarItemConfig) {
        // 通过类名创建控制器实例
        let namespace = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String ?? ""
        let className = "\(namespace).\(config.vc)"
        guard let controllerClass = NSClassFromString(className) as? UIViewController.Type else {
            fatalError("Class \(config.vc) not found")
        }
        
        let vc = controllerClass.init()
//        vc.title = config.title
        
        // 配置 TabBarItem
        let normalImage = UIImage(named: config.image)?
            .withRenderingMode(.alwaysOriginal)
        let selectedImage = UIImage(named: config.selectedImage)?
            .withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem = UITabBarItem(
            title: config.title,
            image: normalImage,
            selectedImage: selectedImage
        )
        vc.tabBarItem.setTitleTextAttributes([.foregroundColor: rgba(0, 0, 0, 0.90)], for: .normal)
        vc.tabBarItem.setTitleTextAttributes([.foregroundColor: rgba(19, 207, 128, 1)], for: .selected)
        
        // 包装导航控制器
        let naVc = KLMNavigationController(rootViewController: vc)
        addChild(naVc)
    }
}
